<?php
/**
 *大话水浒 兵营
 */ 
class Module_overlordbarracks{
	/**
	 * 构造函数
	 */
	function __construct() {
	
	}
	function index(){
		return $this->barracks();
	}
	/**
	 * 兵营
	 * Enter description here ...
	 */
	function barracks(){
		$to_uin = isset($_POST['id']) ? trim($_POST['id']) : '';
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$type_id = isset($_POST['type_id']) ? trim($_POST['type_id']) : '';
		$city = isset($_POST['city']) ? trim($_POST['city']) : '';
		$generals_id = isset($_POST['generals']) ? trim($_POST['generals']) : ''; //传递武将的独立ID
		$task = isset($_POST['task']) ? trim($_POST['task']) : '';
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		//===========================我方的KEY========================
		$key = 'user_' .$uin;//君主信息
		$barracks_key = 'barracks_'.$uin;//兵营信息
		$state_key = 'state_'.$uin;//练兵状态
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		
		// =================================== 查询兵营 ====================================
		$result = App_Overlord_Query::oneSoldiers($uin);

		if (empty($result)){
			$value = array(array('type' => 'infantry', 'count' => 500), array('type' => 'cavalryman', 'count' => 500), array('type' => 'toxotae', 'count' => 500), array('type' => 'special', 'count' => 500));
			App_Overlord_Query::addSoldiers($uin, $value);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=barracks');
			break;
		}
		if (!empty($to_uin) && !empty($type) && !empty($city) && !empty($generals_id)){
			
			$generals_key = 'generals_' .$uin;
			if ($type != 'recover' && $type != 'deploy'){
				//相同就返回主页
				if ($to_uin == $uin) {
			//		header('Location: ' . SITEURL . '/app.php?m=overlorduser&sid=' . sessionInfo('skey') );
			//		break;
				}
			}
			$to_user_info = App_Overlord_Query::oneUser($to_uin);
			if (empty($to_user_info)){
				return View::error('获取对方的资料失败!');
			}
			//查询个人武将表
			$generals_info = App_Overlord_Query::oneUin_Generals($uin);
	
			if (empty($generals_info['Value'])){
				return View::error('你还没有武将.');
			}
			if (empty($generals_info['Value'][$generals_id])){
				return View::error('该名武将不存在.');
			}
			//当是调兵时不判断
			if ($type != 'deploy'){
				if ($generals_info['Value'][$generals_id]['state'] != 0){
					return View::error('该名武将无法出征.');
				}
			}
			//武将可带兵数
			$make_count = intval($generals_info['Value'][$generals_id]['make_count'] / 4);
			$subtract = 0;
			//剩余兵力
			$surplus_barracks = array();
			//初始可带兵
			$make_barracks = array();
			//分兵
			foreach ($result['Value'] as $k => $row){
				if ($row['count'] < $make_count){
					$make_barracks[$k] = $row['count'];
					$subtract = $make_count - $row['count'];
					//剩余兵力0
					$surplus_barracks[$k] = 0;
				}else{
					$make_barracks[$k] = $make_count;
					$surplus_barracks[$k] = $row['count'] - $make_count;
				}
				// 补兵
				if ($subtract > 0){
					foreach ($surplus_barracks as $k => $row){
						if ($row > 0){
							$temp_subtract = min($subtract , $row);
							$surplus_barracks[$k] -= $temp_subtract;
							$make_barracks[$k] += $temp_subtract;
							$subtract -= $temp_subtract;
						}
						if ($subtract <= 0) break;
					}
				}
			}

			//获取时间
			$now_time = App_Overlord_GameStart::getTime();
			$fight_key = 'fight_' .$uin;
			//查询可出征次数
			$fight = App_Overlord_Query::oneCollocate('app_overlord_fight_count' , $fight_key);
			if (empty($fight)){
				$is_value = array('count' => 20, 'time' => $time);
				$fight['Value'] = $is_value;
				//初始化战斗次数
				$code = App_Overlord_Query::addCollocate('app_overlord_fight_count', 'fight_' .$uin, $is_value);
			}
		//判断是否已是第二天
			if($fight['Value']['time'] < $now_time[0]){
				$fight['Value']['time'] = $time;
				$fight['Value']['count'] = 20;
				//如果是第二天修改打斗次数
				App_Overlord_Query::alterCollocate('app_overlord_fight_count' , $fight_key , $fight['Value']);
			}
			$generals_count = App_Overlord_GameStart::getGeneralsBarracksLv($generals_info['Value'][$generals_id]['lv']);
			//获取最少带兵数
			$min_generals_count = $generals_count['count'] * 0.1;
			$smarty->assign('to_uin',$to_uin);
			$smarty->assign('fight',$fight['Value']['count']);
			$smarty->assign('type',$type);
			$smarty->assign('make_barracks',$make_barracks);
			$smarty->assign('generals_info',$generals_info['Value'][$generals_id]);
			$smarty->assign('generals_id',$generals_id);
			$smarty->assign('city',$city);
			$smarty->assign('type_id',$type_id);
			$smarty->assign('min_generals_count',$min_generals_count);
			$smarty->assign('task',$task);
			$smarty->assign('user_info',$to_user_info['Value']);
		}
		
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('君主ID不存在.');
		}
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
	
		//初始兵力
		$barracks_count = 0;
		$is_list = App_Overlord_Query::oneState($uin);
		foreach ($list as $k => $row){
			
			$list[$k]['name'] = $row[$result['Value'][$k]['type']];
			$list[$k]['count'] = $result['Value'][$k]['count'];
			$list[$k]['type'] = $result['Value'][$k]['type'];
			//计算总兵力
			$barracks_count += $result['Value'][$k]['count']; 
		}
	
		if (!empty($is_list)){
			
			if (!empty($is_list['Value']['train_barracks']) && empty($to_uin)){ //有参数不让跳转
				foreach ($is_list['Value']['train_barracks'] as $k => $row){
					if ($row['time'] - $time <= 0){
						$tiem_list[$k] = $row;
						App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=barracks');
					}else{
						$uin_li['train_barracks'][$k] = $is_list['Value']['train_barracks'][$k];
						$is_list['Value']['train_barracks'][$k]['end_time'] = App_Overlord_GameStart::getEndDate($row['time']);
					}
				}
			}
			if (!empty($tiem_list)){
				foreach ($result['Value'] as $k => $row){
					if (!empty($tiem_list[$row['type']])){
						$result['Value'][$k]['count'] += $tiem_list[$row['type']]['count'];  
					
					}
				}
				if (!empty($uin_li)){
					$rs['Value'] = $uin_li;
				} else{
					$rs['Value']['train_barracks'] = '';
				}
				$code = App_Overlord_Query::alterSoldiers($uin , $result['Value']);
				$code = App_Overlord_Query::alterState($uin , $rs['Value']);
			}
	
			$smarty->assign('is_list',$is_list['Value']['train_barracks']);
		}
		$smarty->assign('barracks_count',$barracks_count);
		//兵力总数完成任务
		if (!empty($barracks_count)){
			$smarty->assign('barracks_count',$barracks_count);
			//获取任务类型
//			loadLib('App_Overlord_Task');
//			App_Overlord_Task::trigger('barracks' , array('count' => $barracks_count));		// 触发扩充兵力任务
		}
		
		$count = array(100, 200, 500, 1000, 2000, 5000);
		$smarty->assign('list',$list);
		$smarty->assign('count',$count);
		$smarty->assign('gold',$user_info['Value']['gold']);
		return $smarty->fetch('app/Overlord/barracks.tpl.php');
	}
	/**
	 * 士兵属性
	 * Enter description here ...
	 */
	function barracksattribute(){
		$smarty = loadSmarty();
		$type = isset($_GET['type']) ? trim($_GET['type']) : 'special';
		if (empty($type)){
			return View::error('数据异常!');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$key = 'user_'.$uin;
		$barracks_key = 'barracks_'.$uin;
		$barracks_key = 'barracks_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('君主ID不存在.');
		}
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
		if (empty($list)){
			return View::error('没有该兵种!');
		}
		$result = App_Overlord_Query::oneSoldiers($uin);
		if (empty($result)){
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=barracks');
			break;
		}
		foreach ($list as $k => $row){
			if (!empty($row[$type])){
				$list[$k]['name'] = $row[$result['Value'][$k]['type']];
				$list[$k]['type'] = $result['Value'][$k]['type'];
			}
		}
		if (empty($list)){
			return View::error('没有该兵种!');
		}
		$smarty->assign('list',$list);
		$smarty->assign('type',$type);
		$smarty->assign('country',$user_info['Value']['country']);
		return $smarty->fetch('app/Overlord/barracksattribute.tpl.php');
	}
	/**
	 * 招募士兵
	 */
	function train(){
		$type = isset($_POST['type']) ? trim($_POST['type']) : '';
		$count = isset($_POST['count']) ? intval($_POST['count']) : null;
		if (empty($type) || empty($count)){
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$key = 'user_'.$uin;
		$state_key = 'state_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('君主ID不存在.');
		}
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
		foreach ($list as $k => $row){
			if (!empty($row[$type])){
				$result = $row;
			}
		}
		if (empty($result)){
			return View::error('此兵种不存在.');
		}
		if ($type == 'special'){
			$gold = 8;
		}else{
			$gold = 5;
		}
		$price = $count * $gold;
		if ($price < 0){
			return View::error('非法操作.');
		}
		$user_info['Value']['gold'] -= $price;
		if ($user_info['Value']['gold'] < 0){
			return View::error('你的黄金不足,不能招募士兵');
		}
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$end_time = $count * $result['time'] + $time;
		
		$value = array('count' => $count, 'time' => $end_time);
		$is_list = App_Overlord_Query::oneState($uin);
		
		if (empty($is_list)){
			$is_value = array('train_barracks'=> array($type => $value));
			$code = App_Overlord_Query::addState($uin , $is_value);
		}else{
			if (!empty($is_list['Value']['train_barracks'][$type])){
				return View::error('你已招募该种士兵.');
			}else{
				$is_list['Value']['train_barracks'][$type] = $value;
				$code = App_Overlord_Query::alterState($uin , $is_list['Value']);
			}
		}
		/*//检查任务
		loadLib('App_Overlord_Task');
			
		$task_type = App_Overlord_Task::getTaskType();
		//训练士兵任务
		App_Overlord_Task::getGo('freshman' , $task_type['freshman']['train']);
		if ($type == 'cavalryman'){
			//训练骑兵任务
			App_Overlord_Task::getGo('freshman' , $task_type['freshman']['cavalryman']);
		}*/
		//没训练100个兵2点经验
		$exp = intval($count / 50);
		//至多只能10点
		if ($exp > 10){
			$exp = 10;
		}
		//君主经验
		$user_info['Value']['exp'] += $exp;
		if ($code == 0){
			//修改君主信息
			App_Overlord_Query::alterUser($uin , $user_info['Value']);
		}
		App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=prompt&type='.$type.'&count='.$count.'&exp='.$exp);
	}
	/**
	 * 提示
	 */
	function prompt(){
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$count = isset($_GET['count']) ? intval($_GET['count']) : null;
		$exp = isset($_GET['count']) ? intval($_GET['exp']) : null;
		if (empty($type) || empty($count) || empty($exp)){
			return View::error('参数错误.');
		}
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		$key = 'user_'.$uin;
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('君主ID不存在.');
		}
		//查询兵种
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
		foreach ($list as $k => $row){
			if (!empty($row[$type])){
				$is_list = $row ;
			}
		}
		if (empty($is_list)){
			return View::error('没有此类型兵种.');
		}
		if ($type == 'special'){
			$gold = 8;
		}else{
			$gold = 5;
		}
		$barracks = App_Overlord_Query::oneState($uin);
		if (empty($barracks)){
			return View::error('你还没招募士兵!');
		}
	
		if (empty($barracks['Value']['train_barracks'][$type])){
			return View::error('数据异常!');
		}
		$is_list['time'] = App_Overlord_GameStart::getEndDate($barracks['Value']['train_barracks'][$type]['time']);
		$is_list['type'] = $type;
		$is_list['count'] = $count;
		$is_list['gold'] = $gold * $count;
		$is_list['name'] = $is_list[$type];
		$is_list['exp'] = $exp;
		$smarty = loadSmarty();
		$smarty->assign('list',$is_list);
		return $smarty->fetch('app/Overlord/prompt.tpl.php');
	}
/**
	 * 加速士兵提示
	 */
	function training(){
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
		$id = isset($_GET['id']) ? trim($_GET['id']) : '';
		$uin = SessionInfo('uin');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		loadLib('App_Overlord_Collocate');
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		$data = App_Overlord_Collocate::getGoods();
		//获取详细物品配置
		$data_info = $data[$type][$type_id];
		if(empty($data_info)){
			return View::error('没有该道具!');
		}
		//查询练兵数据
		$state = App_Overlord_Query::oneState($uin);
		
		$user_info = App_Overlord_Query::oneUser($uin);
		if (empty($user_info)){
			return View::error('君主ID不存在.');
		}
		$barracks_info = App_Overlord_Query::oneSoldiers($uin);
		if (empty($barracks_info)){
			return View::error('君主ID不存在.');
		}
		if (!empty($state['Value']['train_barracks'][$id]['time'])){
			if ($state['Value']['train_barracks'][$id]['time'] > $time){
				$brr_time = App_Overlord_GameStart::getEndDate($state['Value']['train_barracks'][$id]['time']);
			}
		}
		$smarty = loadSmarty();
		if (!empty($brr_time)){
			$smarty->assign('brr_time',$brr_time);
		}else {
			$train = array();
			if (!empty($state['Value']['train_barracks'])){
				foreach ($state['Value']['train_barracks'] as $k => $row){
					if ($row['time'] > $time){
						$train['time'] =  App_Overlord_GameStart::getEndDate($row['time']);
						$train['id'] = $k;
						$uin_li['train_barracks'][$k] = $state['Value']['train_barracks'][$k];
					}else{
						$tiem_list[$k] = $row;	
					}
				}
			}
			$smarty->assign('train',$train);
			
			if (!empty($tiem_list)){
				foreach ($barracks_info['Value'] as $k => $row){
					if (!empty($tiem_list[$row['type']])){
						$barracks_info['Value'][$k]['count'] += $tiem_list[$row['type']]['count'];  
					}
				}
				if (!empty($uin_li)){
					$rs['Value'] = $uin_li;
				} else{
					$rs['Value']['train_barracks'] = '';
				}
				$code = App_Overlord_Query::alterSoldiers($uin, $barracks_info['Value']);
				$code = App_Overlord_Query::alterState($uin , $rs['Value']);
			}
		}
		//查询兵种
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
		foreach ($list as $k => $row){
			if (!empty($row[$id])){
				$is_list = $row ;
			}
			if (!empty($train) && !empty($row[$train['id']])){
				$train_brr = $row;
			}
		}
		if (empty($is_list)){
			return View::error('没有该兵种!');
		}
		if (!empty($train_brr)){
			$smarty->assign('train_brr',$train_brr);
		}
		$smarty->assign('id',$id);
		$smarty->assign('is_list',$is_list);
		$smarty->assign('data_info',$data_info);
		return $smarty->fetch('app/Overlord/training.tpl.php');
	}
	/**
	 * 武将攻占
	 */
	function dispatch(){
		$page = isset($_GET['page']) ? intval($_GET['page']) : null;
		$to_uin = isset($_GET['to_uin']) ? trim($_GET['to_uin']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
		$city = isset($_GET['city']) ? trim($_GET['city']) : '';
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		//设置每行10条数据
		$per_page = 10;
		if($page <= 0){
			$page = 1;
		}
		$smarty = loadSmarty();
		$uin = sessionInfo('uin');
		if (empty($uin)){
			return View::error('获取君主ID失败.');
		}
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		if (empty($to_uin) || empty($type) || empty($city)){
			//return View::error('参数错误!');
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordgenerals&a=generals');
		}
		$result = App_Overlord_Query::oneUin_Generals($uin);
		if (!empty($result['Value'])){
			foreach ($result['Value'] as $k => $row){
			//体力恢复
				if (!empty($row['physical_time'])){
			
				 	if ($row['physical_time'] - $time < 0){
				 		$physical_time = 3600;
						//武将有恢复体力技能
						if (!empty($row['skills']['study'])){
							foreach ($row['skills']['study'] as $j => $skills_row){
								if ($j == 'train_hp'){
									//减短体力恢复时间
									$skills_num = $data['study'][$j][$skills_row]['num'];
									$physical_time -= intval($physical_time * $skills_num/100);
								}
							}
						}
					 	$num = intval(($time - $row['physical_time'])/$physical_time) + 1;
					 	$row['physical'] += $num;
					 	if ($row['physical'] < 10){
					 		$result['Value'][$k]['physical'] += $num;
					 		$end_time = ($time - $row['physical_time'])%$physical_time;
					 		$result['Value'][$k]['physical_time'] = $time + $physical_time - $end_time;
					 	}else{
					 		//满10或超过10点体力,体力都是满
					 		$result['Value'][$k]['physical'] = 10;
					 		//给武将恢复体力时间归0
					 		$result['Value'][$k]['physical_time'] = 0;
					 		//显示武将体力时的赋值
					 	}
					}
				}
				if ($row['state'] == 0){
					$row['id'] = $k;
					$list_array[] = $row;
				}
				
				//武将升级所需的经验
				$generals_lv = App_Overlord_GameStart::getGeneralsExp($row['lv']);
				
				if (!empty($generals_lv['upgrade'])){
					if ($row['exp'] >= $generals_lv['upgrade']){
						//武将升级
						$info = App_Overlord_GameStart::getGeneralsPromotion($result['Value'][$k], $generals_lv);
						$result['Value'][$k] = $info;
						$statelv = 1;
					}
				}
			}
		}
	//	print_r($result['Value']);
		if (!empty($num) || !empty($statelv)){
			//修改武将
			$code = App_Overlord_Query::alterUin_generals($uin , $result['Value']);
			App_Overlord_GameStart::getHeader(SITEURL . '/app.php?m=overlordbarracks&a=dispatch&to_uin='.$to_uin.'&type='.$type.'&city='.$city.'&type_id='.$type_id);
		}
		$to_user_info = App_Overlord_Query::oneUser($to_uin);
		if (empty($to_user_info)){
			return View::error('获取对方的资料失败!');
		}
		//获取时间
		$now_time = App_Overlord_GameStart::getTime();
		$fight_key = 'fight_' .$uin;
		//查询可出征次数
		$fight = App_Overlord_Query::oneCollocate('app_overlord_fight_count' , $fight_key);
		if (empty($fight)){
			$is_value = array('count' => 20, 'time' => $time);
			$fight['Value'] = $is_value;
			//初始化战斗次数
			$code = App_Overlord_Query::addCollocate('app_overlord_fight_count', 'fight_' .$uin, $is_value);
		}
		//判断是否已是第二天
		if($fight['Value']['time'] < $now_time[0]){
			$fight['Value']['time'] = $time;
			$fight['Value']['count'] = 20;
			//如果是第二天修改打斗次数
			App_Overlord_Query::alterCollocate('app_overlord_fight_count' , $fight_key , $fight['Value']);
		}
		
		if (!empty($list_array)){
			$count = count($list_array);
			/*******************数据分页******************/
			$page = ($page-1) * $per_page;
			//武将等级从高到低排序
			$generals_max_lv = App_Overlord_GameStart::getGeneralsArrSort($list_array);
					
			$list = array_slice($generals_max_lv,$page,$per_page);		
			$smarty->assign('per_page',$per_page);
			$smarty->assign('count',$count);
			$smarty->assign('list' , $list);
		}
		$smarty->assign('fight',$fight['Value']['count']);
		$smarty->assign('user_info' , $to_user_info['Value']);
		$smarty->assign('to_uin' , $to_uin);
		$smarty->assign('type' , $type);
		$smarty->assign('type_id' , $type_id);
		$smarty->assign('city' , $city);
		return $smarty->fetch('app/Overlord/dispatch.tpl.php');
	}
	/**
	 * 自动补兵
	 */
	function fight_brr(){
		$to_uin = isset($_GET['to_uin']) ? trim($_GET['to_uin']) : '';
		$type = isset($_GET['type']) ? trim($_GET['type']) : '';
		$type_id = isset($_GET['type_id']) ? trim($_GET['type_id']) : '';
		$city = isset($_GET['city']) ? trim($_GET['city']) : '';
		$generals_id = isset($_GET['generals']) ? trim($_GET['generals']) : ''; //传递武将的独立ID
		$task = isset($_GET['task']) ? trim($_GET['task']) : '';
		$smarty = loadSmarty();
		loadLib('App_Overlord_GameStart');
		loadLib('App_Overlord_Query');
		if ($type != 'registration'){
			// ===============================  判断传过来的get参数是否正确 =====================================
			if (empty($to_uin) || empty($type) || empty($city) || empty($generals_id)) {
				return View::error('参数错误');
			}
		}
		$uin = SessionInfo('uin');
		// =================================== 查询兵营 ====================================
		$result = App_Overlord_Query::oneSoldiers($uin);
		if (empty($result)){
			return View::error('数据异常!');
		}
		//查询个人武将表
		$generals_info = App_Overlord_Query::oneUin_Generals($uin);

		if (empty($generals_info['Value'])){
			return View::error('你还没有武将.');
		}
		
		if (empty($generals_info['Value'][$generals_id])){
			return View::error('该名武将不存在.');
		}
		//当是调兵时不判断
		if ($type != 'deploy'){
			if ($generals_info['Value'][$generals_id]['state'] != 0){
				return View::error('该名武将无法出征.');
			}
		}
		//武将可带兵数
		$make_count = intval($generals_info['Value'][$generals_id]['make_count'] / 4);
		$subtract = 0;
		//剩余兵力
		$surplus_barracks = array();
		//初始可带兵
		$make_barracks = array();
		//分兵
		foreach ($result['Value'] as $k => $row){
			if ($row['count'] < $make_count){
				$make_barracks[$k] = $row['count'];
				$subtract = $make_count - $row['count'];
				//剩余兵力0
				$surplus_barracks[$k] = 0;
			}else{
				$make_barracks[$k] = $make_count;
				$surplus_barracks[$k] = $row['count'] - $make_count;
			}
			// 补兵
			if ($subtract > 0){
				foreach ($surplus_barracks as $k => $row){
					if ($row > 0){
						$temp_subtract = min($subtract , $row);
						$surplus_barracks[$k] -= $temp_subtract;
						$make_barracks[$k] += $temp_subtract;
						$subtract -= $temp_subtract;
					}
					if ($subtract <= 0) break;
				}
			}
		}
		if (empty($make_barracks)){
			return View::error('数据异常!');
		}
		$brr_count = array_sum($make_barracks);
		if (empty($brr_count)){
			return View::error('你的兵力不足!' , 0 , array(
						array('name' => '去招募' , 'url' => '/app.php?m=overlordbarracks&amp;a=barracks&amp;sid=' . sessionInfo('skey')),
					));
		}
		$user_info = App_Overlord_Query::oneUser($uin);
		$list = App_Overlord_GameStart::getBarracksType($user_info['Value']['country']);
		if (empty($list)){
			return View::error('没有该兵种!');
		}
		foreach ($make_barracks as $k => $row){
			$list[$k]['count'] = $row;
			$list[$k]['name'] = $list[$k][$result['Value'][$k]['type']];
		}
		$smarty = loadSmarty();
		$smarty->assign('to_uin',$to_uin);
		$smarty->assign('type',$type);
		$smarty->assign('list',$list);
		$smarty->assign('type_id',$type_id);
		$smarty->assign('generals_info',$generals_info['Value'][$generals_id]);
		$smarty->assign('city',$city);
		$smarty->assign('generals_id',$generals_id);
		$smarty->assign('task',$task);
		$smarty->assign('make',$make_barracks);
		return $smarty->fetch('app/Overlord/fight_brr.tpl.php');
	}
}

# end of script